package cn.lena.idle;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import cn.lena.idle.adapter.MyCollectionAdapter;
import cn.lena.idle.dao.CatagoryDbHelper;
import cn.lena.idle.dao.CollectionDbHelper;
import cn.lena.idle.dao.GoodsDbHelper;
import cn.lena.idle.pojo.Catagory;
import cn.lena.idle.pojo.Collection;
import cn.lena.idle.pojo.DTO.CurrentGoods;
import cn.lena.idle.pojo.DTO.LoginUser;
import cn.lena.idle.pojo.DTO.Version;
import cn.lena.idle.pojo.Goods;
import cn.lena.idle.util.DateUtils;

/**
 * @author lena
 * @date 2021/6/11
 */
public class IndexActivity extends AppCompatActivity {

    private ImageButton myself;
    private Button chooseCatagory;
    private TextView title;
    private ListView lv;

    private List<Catagory> catagories;
    private List<Goods> goods;
    private PopupMenu pm;
    private GoodsDbHelper goods_db;
    private MyCollectionAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);

        chooseCatagory=(Button)findViewById(R.id.chooseCatagory);
        myself=(ImageButton)findViewById(R.id.myself);
        lv=(ListView)findViewById(R.id.lv_index);
        title=(TextView)findViewById(R.id.index_title);
        adapter=new MyCollectionAdapter(getApplicationContext());

/*
        // 查询当前用户是否登录，如果没有登录跳转到登录页面
        if( LoginUser.CURRENT_USER!=null ){
            startActivity(new Intent(IndexActivity.this,LoginUser.class));
            IndexActivity.this.finish();
        }
*/

        // 初始化页面
        // 1.查询出所有商品，显示在页面上
        initGoods();

        // 2.为分类按钮添加下拉菜单项
        initMenu();
        chooseCatagory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 显示菜单项
                pm.show();
            }
        });


        // 3.添加图标单击跳转到个人中心页面
        myself.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转到个人中心页面
                Intent intent=new Intent(IndexActivity.this,MyActivity.class);
                startActivity(intent);
            }
        });

        // 进入商品详情页
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                CurrentGoods.SELECT=(Goods)adapter.getItem(position);
                Intent intent=new Intent(IndexActivity.this,GoodsDetailActivity.class);
                startActivity(intent);
            }
        });


        // 长按商品收藏
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                collectGoods(position);
                // 返回值如果是false，执行完长按事件还会执行单击事件；如果是true，执行完长按事件不会再执行单击事件。
                return true;
            }
        });


    }

    /**
     * 收藏商品
     * @param position
     */
    private void collectGoods(final int position){
        AlertDialog.Builder builder = new AlertDialog.Builder(IndexActivity.this);
        builder.setTitle("提示:").setMessage("确定要收藏吗?").setIcon(R.drawable.icon_my).setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        }).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                Goods g=(Goods)adapter.getItem(position);
                CollectionDbHelper collection_db=new CollectionDbHelper(getApplicationContext(),CollectionDbHelper.DB_NAME,null,Version.COLLECTION);
                Collection c=new Collection();
                c.setUserId(LoginUser.CURRENT_USER.getId());
                c.setGoodsId(g.getId());
                c.setAddDate(DateUtils.getNowDate2());
                int result=collection_db.add(c);
                if(result>0){
                    Toast.makeText(IndexActivity.this,"收藏成功！",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(IndexActivity.this,"收藏失败！",Toast.LENGTH_SHORT).show();
                }
            }
        }).show();
    }


    /**
     * 当选择某一个分类时，更新页面
     * @param item
     */
    private void changeActivity(MenuItem item){
        if(item.getItemId() == 4719) {
            // 所有分类
            initGoods();
            title.setText("首页");
        }else{
            goods=goods_db.getByCatagoryId(item.getItemId());
            // 刷新页面
            adapter=new MyCollectionAdapter(getApplicationContext());
            adapter.setData(goods);
            lv.setAdapter(adapter);
            // 更新页面标题
            title.setText(item.getTitle());
        }
    }

    /**
     * 初始化页面列表 t
     */
    private void initGoods(){
        goods_db=new GoodsDbHelper(getApplicationContext(),GoodsDbHelper.DB_NAME,null,Version.GOODS);
        goods=goods_db.getAll();
        // 装载进adapter
        adapter.setData(goods);
        lv.setAdapter(adapter);
    }

    /**
     * 初始化分类菜单项 t
     * 设置单击事件
     */
    private void initMenu(){
        pm=new PopupMenu(IndexActivity.this,chooseCatagory);
        Menu menu=pm.getMenu();
        pm.getMenuInflater().inflate(R.menu.catagory,menu);
        // 查询所有分类
        findAll();
        // 添加“所有”分类
        menu.add(0,4719,0,"所有");
        // 将分类全部添加到菜单项中
        for(Catagory c:catagories){
            // 1-组别、2-菜单项id、3-菜单项顺序、4-菜单项内容
            menu.add(0,c.getId(),0,c.getName());
        }
        // 添加单击数据项事件
        pm.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // 当选择某一个分类的时候，更新页面值
                changeActivity(item);
                return true;
            }
        });
    }


    /**
     * 查询所有分类 t
     */
    private void findAll(){
        CatagoryDbHelper catagory_db=new CatagoryDbHelper(getApplicationContext(),CatagoryDbHelper.DB_NAME,null, Version.CATAGORY);
        catagories = catagory_db.getAll();
    }

}
